package com.lab303.powerproject.repository;

import com.lab303.powerproject.entity.Device;
import com.lab303.powerproject.pojo.CustomAtlasDevice;
import com.lab303.powerproject.pojo.CustomDevice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface DeviceRepository extends JpaRepository<Device,Integer> {

    @Query(nativeQuery = true ,value = "select * from device where type_id = ?1")
    List<Device> selectDevicesByTypeId(Integer typeId);

    @Query(value = "select new com.lab303.powerproject.pojo.CustomDevice(d.deviceId,d.deviceName) from Device d")
    List<CustomDevice> selectAllDevices();

    List<Device> findDevicesByDeviceNameEquals(String deviceName);

    @Query(value = "select new com.lab303.powerproject.pojo.CustomAtlasDevice(d.deviceId,d.deviceName,d.picPath,d.highlightPicPath,d.xCoordinates,d.yCoordinates) from Device d")
    List<CustomAtlasDevice> selectAtlasDevice();

    @Query(value = "select new com.lab303.powerproject.pojo.CustomAtlasDevice(d.deviceId,d.deviceName,d.picPath,d.highlightPicPath,d.xCoordinates,d.yCoordinates) from Device d where d.deviceId=:deviceId")
    CustomAtlasDevice selectAtlasDeviceByDeviceId(@Param("deviceId") Integer deviceId);

    @Modifying
    @Query(value = "update Device d set d.xCoordinates = ?1,d.yCoordinates = ?2 where d.deviceId = ?3")
    int updateDeviceCoordinates(float x,float y,int id);

    @Modifying
    @Query(value = "update Device d set d.xCoordinates = ?1,d.yCoordinates = ?2,d.isInGraph = ?3 where d.deviceId = ?4")
    int updateDeviceCoordinatesAndIsInGraph(float x,float y,int isInGraph,int id);

    @Modifying
    @Query(value = "update Device d set d.isInGraph = ?1 where d.deviceId = ?2")
    int updateDeviceIsInGraph(int isInGraph,int id);

    @Query(value = "select new com.lab303.powerproject.pojo.CustomDevice(d.deviceId,d.deviceName) from Device d where d.deviceId = ?1")
    CustomDevice selectCustomDevice(Integer deviceId);

    @Query(value = "select new com.lab303.powerproject.pojo.CustomDevice(d.deviceId,d.deviceName) from Device d where d.graphId = ?1")
    List<CustomDevice> findDevicesByGraphId(Integer graphId);

    List<Device> findDevicesByGraphIdEquals(Integer graphId);

    @Query(value = "select new com.lab303.powerproject.pojo.CustomAtlasDevice(d.deviceId,d.deviceName,d.picPath,d.highlightPicPath,d.xCoordinates,d.yCoordinates) from Device d where d.graphId = ?1")
    List<CustomAtlasDevice> selectAtlasDeviceByGraphId(Integer graphId);

    void deleteDevicesByGraphId(Integer graphId);

    List<Device> findDevicesByGraphIdAndIsInGraph(Integer graphId,Integer isInGraph);



}
